package com.yzd.learnjava.concurrency;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.yzd.learnjava.util.Print;

public class EvenChecker implements Runnable{
	private IntGenerator generator;
	private final int id ;
	public EvenChecker(IntGenerator generator, int ident) {
		this.generator = generator;
		this.id = ident;
	}
	
	public void run() {
		while(!generator.isCanceled()) {
			int val = generator.next();
			if(val % 2 != 0) {
				Print.print(val + " not even");
				generator.cancel();
			}
		}
	}
	
	public static void test(IntGenerator gp , int count) {
		Print.print("Press Control-C to Exit");
		ExecutorService exec = Executors.newCachedThreadPool();
		for(int i = 0 ; i < count; i++) {
			exec.execute(new EvenChecker(gp, i));
		}
		//exec.shutdown();
	}
	
	public static void test(IntGenerator gp) {
		test(gp,10);
	}
}
